/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.fakturak;

import igraf.ItzuliInterfazea;
import igraf.Panela;
import igraf.fakturak.javapdf.javapdf;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.PerituFakt;

public class PerituFaktura extends Panela {
	private static final long serialVersionUID = -6887932888834833305L;

	private JLabel zenbaki, zenbakia, dirua;
	private JTextField zenb, zenba, diru;
	private JButton ok, ezeztatu, aurkitu;
	private PerituFakt perit = new PerituFakt();

	public PerituFaktura(String tituloa, final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());
		// Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(9, 1));

		JPanel ge = new JPanel();
		ge.setBackground(SystemColor.activeCaption);
		erdikoa.add(ge);

		JPanel ce = new JPanel();
		ce.setBackground(SystemColor.activeCaption);
		erdikoa.add(ce);
		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbaki = new JLabel("Istripu zenbakia:");
		zenbaki.setFont(new Font("Arial", Font.PLAIN, 14));
		zenb = new JTextField(8);
		aurkitu = new JButton("Bilatu");
		aurkitu.setIcon(new ImageIcon(PerituFaktura.class
				.getResource("/Argazkia/lupa-32.png")));
		arri.add(zenbaki);
		arri.add(zenb);
		arri.add(aurkitu);
		erdikoa.add(arri);

		JPanel arrib = new JPanel();
		arrib.setBackground(SystemColor.activeCaption);
		FlowLayout fl_carri = new FlowLayout();
		fl_carri.setHgap(15);
		fl_carri.setVgap(10);
		arrib.setLayout(fl_carri);
		zenbakia = new JLabel("Peritu zenbakia:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		zenba = new JTextField(8);
		arrib.add(zenbakia);
		arrib.add(zenba);
		erdikoa.add(arrib);

		JPanel xx = new JPanel();
		xx.setBackground(SystemColor.activeCaption);
		FlowLayout fl = new FlowLayout();
		fl.setHgap(15);
		fl.setVgap(10);
		xx.setLayout(fl);
		dirua = new JLabel("Zenbatekoa:");
		dirua.setFont(new Font("Arial", Font.PLAIN, 14));
		diru = new JTextField(8);
		xx.add(dirua);
		xx.add(diru);
		erdikoa.add(xx);

		JPanel e = new JPanel();
		e.setBackground(SystemColor.activeCaption);
		erdikoa.add(e);

		JPanel eh = new JPanel();
		eh.setBackground(SystemColor.activeCaption);
		erdikoa.add(eh);

		JPanel c = new JPanel();
		c.setBackground(SystemColor.activeCaption);
		erdikoa.add(c);

		// beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setVgap(15);
		flowLayout.setHgap(110);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Faktura sortu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
		aurkitu.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				String zer = zenb.getText();
				if (zer.isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Sartu istripu bat zuriunean, mesedez!", "Oker",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						if (perit.badagoIstripua(zer)) {

							String idTail = perit.getPeriFakDatuak(zer);
							if (idTail != null) {
								ezarri(false);
								zenba.setText(idTail);
							}
						} else {
							JOptionPane.showMessageDialog(null,
									"Ez da existitzen sartu duzuna", "Oker",
									JOptionPane.ERROR_MESSAGE);
						}
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}

		});
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaitIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (!perit.badagoIstripua(zenb.getText())) {
						JOptionPane.showMessageDialog(null,
								"Ez da Istripu hori aurkitzen", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else {
						try {
							int zenbatekoa = Integer.parseInt(diru.getText());
							perit.PeriFaktSortu(zenb.getText(), zenbatekoa);
							JOptionPane.showMessageDialog(null,
									"Ondo sortu da faktura", "",
									JOptionPane.INFORMATION_MESSAGE);

							javapdf pdf = new javapdf();

							pdf.crear_PDF(
									"Peritu Faktura: " + zenba.getText() + " ",
									"OpenSecur",
									"",
									"",
									"Egun on adiskide: \n\n "
											+ "Gure aseguru-etxea empresa komunikatzen dizu harro dagoela gure enpresarentzako prestatutako zerbitzuekin. Hurrengoa ´"
											+ zenb.getText()
											+ "´ istripuaren fakturazioa da. Bertan zuk, ´"
											+ zenba.getText()
											+ "´ peritu NIF-ekin, parte hartu zenuen gure bezeroaren kotxea baloratzen. Eskertzen dizugu zure lana. Hurrengo egunetan "
											+ diru.getText()
											+ " €-ko txekea jasoko dituzu zure helbidean. \n\n "
											+ "Agur bero bat: \n\n\n"
											+ " Enpresako zuzendaritza");

							eremuakGarbitu();
						} catch (NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu zenbatekoa eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}
				} catch (Exception e) {
					JOptionPane
							.showMessageDialog(
									null,
									" Errepikatuta dago faktura hau.\n "
											+ "Begiratu ea eremuren bat ez dagoen errepikaturik ",
									"Oker", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		});
	}

	private boolean zerbaitIdatzi() {
		boolean aurki = false;

		aurki = zenb.getText().length() > 0;
		return aurki;
	}

	private void ezarri(boolean balioa) {
		zenb.setEnabled(balioa);

		zenba.setEnabled(!balioa);
		diru.setEnabled(!balioa);

		ok.setEnabled(!balioa);
	}

	@Override
	public void eremuakGarbitu() {
		zenb.setText("");
		zenba.setText("");
		diru.setText("");
		ezarri(true);
	}

}
